package source.events.action;

import source.model.*;
import source.model.type.*;
import source.view.*;

public class PickupItemAction implements Action{
	
	public PickupItemAction(){
		
	}
	/*
	 * (non-Javadoc)
	 * @see source.events.action.Action#execute(source.model.DataHandler)
	 */
	
	public ActionResult execute(DataHandler dh){
		GameObject executor=(Unit)dh.getCurrentSelection();
		if(!executor.objectType().equals(GameObjectType.UNIT))
			return ActionResult.IMPOSSIBLE;
		Unit u=(Unit)executor;
		if(u.getMissionFlag())
		{
			MainScreen.writeToConsole("Current selection is on a mission");
			return ActionResult.IMPOSSIBLE;
		}
		Position p=dh.getCurrentPosition();
		GameObject[] go=dh.positionQuery(p, GameObjectType.ITEM);
		if(go.length==0)
			return ActionResult.IMPOSSIBLE;
		Item item=(Item)go[0];
		if(item.isObstruction())
			return ActionResult.IMPOSSIBLE;
		System.out.println("PUIA: " + item.objectID().getID());
		u.addItem(item);
		MainScreen.writeToConsole("You have picked up " +item + ". " + item.msg());
		dh.unassign(item);
		dh.returnObject(executor);
		return ActionResult.SUCCESS;
	}
}
